package RentCarProject;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class UserBase {
    String username;
    String password;
    int account=0;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAccount() {
        return account;
    }

    public void setAccount(int account) {
        this.account = account;
    }

    public boolean loGin() throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from user where username=? and password=? ";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            this.account=resultSet.getInt("account");
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return true;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return false;
        }
    }

    public boolean isreGister() throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from user where username=? ";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return true;
        }else{
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return false;
        }
    }

    public boolean reGister() throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="insert into user(username,password) values (?,?)";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        preparedStatement.setString(2,password);
        int count=preparedStatement.executeUpdate();
        if(count>0){
            preparedStatement.close();
            connection.close();
            return true;
        }else{
            preparedStatement.close();
            connection.close();
            return false;
        }
    }

    public void minAccount(int minus) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="update user set account=account-? where username=? and password=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setInt(1,minus);
        preparedStatement.setString(2,username);
        preparedStatement.setString(3,password);
        preparedStatement.executeUpdate();
        account-=minus;
        preparedStatement.close();
        connection.close();
    }

    public void addAccount(int add) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="update user set account=account+? where username=? and password=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setInt(1,add);
        preparedStatement.setString(2,username);
        preparedStatement.setString(3,password);
        preparedStatement.executeUpdate();
        account+=add;
        preparedStatement.close();
        connection.close();
    }

    public void userCharge(int add) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="insert into charge values(?,current_time,?)";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,getUsername());
        preparedStatement.setInt(2,add);
        preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
    }

}
